Skip to content

Conversation

EnzoPB
Copy link
Contributor

@EnzoPB EnzoPB commented Sep 23, 2025

This PR fixes issue #79 where the IMU interrupt was not getting triggered.

Now right after attachInterrupt, HAL_NVIC_SetPriority is called to set the interrupt priority to the maximum allowed while still allowing FreeRTOS API calls inside the ISR.

#ifdef ARDUINO_ARCH_STM32
// (#79) on stm32, increase the interrupt priority, otherwise it does not get called
// configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY is the maximum priority allowed for interrupts that needs to call FreeRTOS api
HAL_NVIC_SetPriority(hal_get_irqn_from_pin(interrupt_pin), 0, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this works, but does not make sense to me. See

https://stackoverflow.com/questions/50243996/what-are-valid-values-of-hal-nvic-setpriority-when-using-stm32-and-freertos

as far as I understand it configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY is a interrupt priority, but in the HAL_NVIC_SetPriority call it is used as a subpriority.

Why not use HAL_NVIC_SetPriority(hal_get_irqn_from_pin(interrupt_pin), 0, 0); i.e. use the highest priority?

Or use HAL_NVIC_SetPriority(hal_get_irqn_from_pin(interrupt_pin), configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY, 0);, but I'm not sure if this will fix the issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I mixed up preempt priority & subpriority. It worked because the preempt priority was 0 (the highest).

HAL_NVIC_SetPriority(hal_get_irqn_from_pin(interrupt_pin), configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY, 0); doesn't work, which seems logical since configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY's value is already the default preempt priority.

Why not use HAL_NVIC_SetPriority(hal_get_irqn_from_pin(interrupt_pin), 0, 0); i.e. use the highest priority?

Because using a priority higher than configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY will not allow the ISR to call FreeRTOS functions (FreeRTOSConfig_Default.h).
It looks like no FreeRTOS calls are made in the imu ISR, so it's ok to use a priority > configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY, but that is something to remember for future development.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently on STM32 the whole imu interrupt (including imu_loop() ) runs in interrupt context. So probably will have issues as soon as SDCARD (uses separate task) is implemented for STM32.

On ESP32/RP2 the imu stuff runs as high priority task, which is notified by the imu interrupt. I could not get this to work on STM32, but I did not make a big effort on this...

Anyway - to move ahead and get at least the interrupt working again:

Let's go with HAL_NVIC_SetPriority(hal_get_irqn_from_pin(interrupt_pin), 0, 0), as this will trigger the imu interrupt for sure.

Maybe enclose everything (including IRQn_Type hal_get_irqn_from_pin(int pin)) with #ifdef MF_STM32_INTERRUPT_HACK instead of #ifdef ARDUINO_ARCH_STM32 and set #define MF_STM32_INTERRUPT_HACK 1 at the beginning of in hal_stm32_cpp.h with some comments. Makes it easier to find this hack back later.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe enclose everything (including IRQn_Type hal_get_irqn_from_pin(int pin)) with #ifdef MF_STM32_INTERRUPT_HACK instead of #ifdef ARDUINO_ARCH_STM32 and set #define MF_STM32_INTERRUPT_HACK 1 at the beginning of in hal_stm32_cpp.h with some comments. Makes it easier to find this hack back later.

This won't worlk - leave as is...

@qqqlab
Copy link
Owner

qqqlab commented Sep 23, 2025

Super! Many thanks for your PRs!

@qqqlab qqqlab merged commit 59a1a0d into qqqlab:main Sep 23, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants